
import AnimationObject from "./animationobj"
import GameConst from "./GameConst"
import TweenData from "./TweenData"
import Utils from "./utils"
import SoundsManager from "./soundmanager";
import BonusDestroyAnimation from "./amn/BonusDestroyAnimation";
import SinglePlayObject from "./amn/SinglePlayObject";
export default class FieldObject extends AnimationObject{
    constructor(colorType) {
        super(GameConst.jellyAnimation);
        this.isMoving = true;
        this.SPEED = 200;
        this.lastTarget = new Laya.Point();
        this.currentTile = 6;
        this.bonusType = 0;
        this.isActive = false;
        this.isCustom = false;
        this.moveCornerCount = 0;
        this.timeSinceStop = 0;
        this.isMovedAfterCorner = false;
        this.rowMoveAfterConterCount = 0;
        this.nextIdleTime = 0;
        this.timeSinceAnim = 0;
        this.isCached = false;
        this.playBonusAnimationIn = -1;
        this.isHighValue = false;
        this.highValueIn = -1;
        this.pos = new Laya.Point();
        this.tween = new TweenData;
        this.prevNeighbours = [null, null, null, null];
        this.colorType = colorType;
        this.sprite = Utils.createSpriteFromSpritesheet(this.getFileName());
        this.sprite.name = this.getFileName();
        this.sprite.gotoAndStop(this.getFileName());
        // var n = this.sprite.getBounds();
        // console.log(this.sprite.name+n);
        // this.sprite.pivotX = this.sprite.width / 2;
        // this.sprite.pivotY = this.sprite.height / 2;
        this.stopFrame = 9;
    }

    init(n, r, i) {
        super.initAnim();
        this.cell = n;
        n.object = this;
        this.isHighValue = false;
        Laya.Tween.clearTween(this.pos);
        Laya.Tween.clearTween(this.sprite);
        GameConst.level = GameConst.level;
        Utils.addChild(this.sprite, GameConst.level.gemLayer);
        this.pos.x = r;
        this.pos.y = i;
        this.sprite.x = r;
        this.sprite.y = i;
        this.isMoving = false;
        this.isWaitingForDestruction = false;
        this.isPlaying = false;
        this.currentFrame = 0;
        this.isMoving = false;
        this.sprite.visible = true;
        this.stopSpeed = 1;
        this.setNextIdleTime();
        this.isCached = false;
        this.playBonusAnimationIn = -1;
        this.bonusType = 0;
        this.sprite.gotoAndStop(this.getFileName());
        this.setFrame(-1, true);
        // var s = this.sprite.getBounds();
        this.sprite.pivotX = this.sprite.width / 2;
        this.sprite.pivotY = this.sprite.height / 2;
        //console.log(this.sprite.name+s);
        this.highValueIn = -1;
        if (!this.isActive) {
            FieldObject.activeCount++;
            this.isActive = true;
        }
        return this;
    };

    onStop() {
        if (this.teleportTarget) {
            var r = this.cell.teleportCellStart;
            // this.cell.teleportSprite.gotoAndPlay("teleport_out");
            this.cell.teleportSprite.play(0,false);
            SoundsManager.instance.playTeleport();
            this.setPos(this.cell.pos.x, this.cell.pos.y);
            var i = this.teleportTarget != this.cell;
            r.object = this.cell.object = null;
            this.isMoving = false;
            if (i) {
                this.moveTo(this.teleportTarget.pos.x, this.teleportTarget.pos.y, false, .05);
            } else {
                this.playJellyAnimation(this.stopSpeed, .75 + Math.random() * .5);
            }
            this.teleportTarget.setObject(this);
            this.teleportTarget = null;
        }
    };

    update(t) {
        if (!this.isMoving) {
            this.timeSinceStop += t;
            if (this.timeSinceStop >= .5)
                this.moveCornerCount = 0;
        }
        this.isMoving = this.isMoving || this.teleportTarget != null;
        if (this.sprite) {
            if (this.isPlaying)
                super.update(t);
            else {
                this.timeSinceAnim += t;
                if (this.timeSinceAnim >= this.nextIdleTime) {
                    this.setNextIdleTime();
                }
            }
            if (this.isMoving && this.teleportTarget == null)
                this.tween.update(t, this);
            if (this.playBonusAnimationIn >= 0) {
                this.playBonusAnimationIn -= t;
                if (this.isBonus && this.playBonusAnimationIn < 0) {
                    this.playBonusAnimation();
                }
            }
            if (this.highValueIn >= 0) {
                this.highValueIn -= t;
                if (this.highValueIn < 0)
                    this.setHighValue();
            }
            this.sprite.x = this.pos.x + this.animPos.x;
            this.sprite.y = this.pos.y + this.animPos.y;
        }
    }
    getFileName() {
        if (this.isPushable)
            return "cupcake";
        if (this.bonusType == 4)
            return "bonus_color";
        var btype = FieldObject.bonusNames[this.bonusType];
        var name = FieldObject.assetNames[this.colorType];
        if(btype){
            name = name+btype;
        }
        
        if (!this.isBonus && this.isHighValue){
            name += " bonus";
        }
        return name;
    }
}
    FieldObject.prototype.setNextIdleTime = function () {
        this.nextIdleTime = 1 + Math.random() * 78;
        this.timeSinceAnim = 0;
    };
    
    FieldObject.prototype.setHighValue = function () {
        if (this.isBonus)
            return;
        this.isHighValue = true;
        this.sprite.gotoAndStop(this.getFileName());
        // this.sprite.gotoAndStop(0);
        this.playJellyAnimation(1, 1);
    };
    FieldObject.prototype.setHighValueIn = function (e) {
        this.highValueIn = e;
    };
    Object.defineProperty(FieldObject.prototype, "isPushable", {
        get: function () {
            return this.colorType == 4;
        },
        enumerable: true,
        configurable: true
    });
    
    FieldObject.prototype.release = function () {
        Utils.removeClip(this.sprite);
        this.sprite.visible = false;
        this.highValueIn = -1;
        Laya.Tween.clearAll(this.pos);
        Laya.Tween.clearAll(this.sprite);
        this.isMoving = false;
        this.isWaitingForDestruction = false;
        this.animPos.x = this.animPos.y = 0;
        this.setFrame(-1);
        this.isHighValue = false;
        this.isPlaying = false;
        this.currentFrame = 0;
        this.playBonusAnimationIn = -1;
        this.isMoving = false;
        this.animationPower = 1;
        this.stopSpeed = 1;
        this.animationSpeed = 1;
        this.isCached = false;
        this.bonusType = 0;
        if (this.isActive) {
            FieldObject.activeCount--;
            this.isActive = false;
        }
        return this;
    };
    Object.defineProperty(FieldObject.prototype, "isBonus", {
        get: function () {
            return this.bonusType != 0;
        },
        enumerable: true,
        configurable: true
    });
    FieldObject.prototype.setBonusType = function (e) {

        if (this.bonusType != e) {
            this.isHighValue = false;
            this.bonusType = e;
            var t = e == 4 ? 4 : e == 3 ? 5 : e == 1 ? 2 : 3;
            this.sprite.gotoAndStop(this.getFileName());

            var n = this.sprite;
            this.sprite.pivotX = n.width / 2;
            this.sprite.pivotY = n.height / 2;
            this.playJellyAnimation(1, 1);
        }
    };
    FieldObject.prototype.onSpellExplosion = function (e) {
        if (typeof e === "undefined") {
            e = -1;
        }
        this.sprite.visible = false;
    };
    FieldObject.prototype.isSwapable = function () {
        return true;
    };
    FieldObject.prototype.moveTo = function (e, t, n, r, i) {
        if (typeof n === "undefined") {
            n = false;
        }
        if (typeof r === "undefined") {
            r = 0;
        }
        if (typeof i === "undefined") {
            i = false;
        }
        this.isMoving = true;
        var s = n ? .5 : 1;
        this.lastTarget.x = e;
        this.lastTarget.y = t;
        this.timeSinceStop = 0;
        if (!i && this.isMovedAfterCorner) {
            this.rowMoveAfterConterCount++;
            if (this.rowMoveAfterConterCount > 1) {
                this.rowMoveAfterConterCount = 0;
                this.isMovedAfterCorner = false;
            }
        } else
            this.rowMoveAfterConterCount = 0;
        this.isMovedAfterCorner = false;
        var o = 300 * Math.pow(.6, Utils.limit(this.moveCornerCount, 0, 4));
        this.moveCornerCount++;
        var u = Utils.distanceBetweenPoints(e, t, this.pos.x, this.pos.y);
        if (u < 1)
            o = 10;
        if (i) {
            var a = GameConst.level.stageToGridX(this.sprite.x);
            var f = GameConst.level.stageToGridY(this.sprite.y);
            a = Math.floor(a);
            f = Math.floor(f);
            for (var l = f - 1; l >= 0; --l)
                if (this.validatePos(a, l)) {
                    var c = GameConst.level.cells[a][l];
                    if (c && c.object && !c.object.isMoving)
                        c.object.isMovedAfterCorner = true;
                }
        }
        this.tween.init(this.pos.x, this.pos.y, e, t, o / 1e3, i);
        if (!this.isPushable && u > 10) {
            this.stopSpeed = .7 + Math.random() * .3;
            this.playAnimation(0, .6);
        }
    };
    FieldObject.prototype.setPos = function (e, t) {
        this.pos.x = e;
        this.pos.y = t;
    };

    FieldObject.prototype.playJellyAnimation = function (e, t) {
        var n = Utils.getInt(3);
        this.playAnimation(n == 0 ? 11 : n == 1 ? 39 : 67, e, t);
    };
    FieldObject.prototype.stopMove = function () {
        if (this.teleportTarget == null)
            this.isMoving = false;
        this.timeSinceStop = 0;
        if (!this.isPushable && this.isPlaying) {
            this.playJellyAnimation(this.stopSpeed, .75 + Math.random() * .5);
        }
        if (this.isPushable && this.cell.y == GameConst.level.fieldHeight - 1) {
            GameConst.level.objects.push(GameConst.pool.getText("100", this.cell, 0));
            var t = GameConst.level.gridToStageX(this.cell.x);
            var n = GameConst.level.gridToStageY(GameConst.level.fieldHeight);
            Laya.Tween.to(this.pos, {
                x: t,
                y: n
            }, 300, Laya.Ease.cubicIn,Laya.Handler.create (this,this.pushDown));
            SoundsManager.instance.playSound("cake_down");
        } else
            SoundsManager.instance.playSound("stop_move");
    };
    FieldObject.prototype.pushDown = function () {
        this.cell.object = null;
        GameConst.level.target.onTargetPush();
        GameConst.pool.returnObject(this);
    };
    FieldObject.prototype.playBonusAnimation = function () {
        if (this.bonusType == 4){
            GameConst.level.objects.push(new SinglePlayObject(this.sprite.x, this.sprite.y, "color_bonus_anm",  GameConst.level.gemDestroyLayer, 67, 61, GameConst.ANIM_SCALE));
        }
        else {
            GameConst.level.objects.push(new BonusDestroyAnimation(this.sprite.x, this.sprite.y, this.getFileName(), this.cell));
        }
        this.sprite.visible = false;
    };
    FieldObject.prototype.validatePos = function (e, t) {
        return e >= 0 && t >= 0 && e < GameConst.level.fieldWidth && t < GameConst.level.fieldHeight;
    };
    FieldObject.prototype.getNeighbourObject = function (e, t, n) {
        var r = this.cell.x + e;
        var i = this.cell.y + t;
        if (r < 0 || r >= GameConst.level.fieldWidth)
            return null;
        if (!this.isMoving) {
            if (!this.validatePos(r, i))
                return null;
            o = GameConst.level.cells[r][i].object;
            if (o && !o.isMoving && o.colorType == this.colorType)
                return o;
            else
                return null;
        } else {
            for (var s = -1; s < GameConst.level.fieldHeight; s++) {
                if (s != -1 && !this.validatePos(r, s))
                    continue;
                var o = s == -1 ? this.prevNeighbours[n] : GameConst.level.cells[r][s].object;
                if (o && !o.isDestroyed && o.colorType == this.colorType) {
                    var u = Math.abs(o.sprite.x - (this.sprite.x + e * GameConst.level.tileSize)) < .5;
                    var a = Math.abs(o.sprite.y - (this.sprite.y + t * GameConst.level.tileSize)) < .5;
                    if (u && a) {
                        return o;
                    }
                }
            }
        }
        return null;
    };
    FieldObject.TILE_SIZE = 35;
    FieldObject.destroyCount = 0;
    FieldObject.GEM_SCALE = 1;

    FieldObject.assetNames = ["blue", "green", "purple", "orange"];
    FieldObject.targetNames = ["蓝色", "绿色", "紫色", "黄色"];
    FieldObject.bonusNames = ["", " horizontal", " vertical", " bomb", " bomb"];
    FieldObject.activeCount = 0;
